package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.DianZhuHuoDongQuanData;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.DZHDQListAdapter;
import com.zbbt.sportslottery.ui.adapter.DZHDQListAdapter.OnBottomClickListener;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.DialogUtil;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.TimeUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.DialogUtil.OnDatePickedListener;

public class ExchangeRecordActivity extends SimpleBaseActivity {
	private List<DianZhuHuoDongQuanData> DZHDQdata = new ArrayList<DianZhuHuoDongQuanData>();
	private DZHDQListAdapter listAdapter = null;
	private int curPage = 1;
	private int pageSize = 10;
	private boolean hasMore = false;
	private ProgressBarCircularIndetermininate exchangeprogressBar;
	private ListView mListView;
	private RadioButton already_btn;
	private RadioButton unalready_btn;
	private TextView tv_start;
	private TextView tv_end;
	private String startTime = "2000-01-01";
	private String endTime = "2100-01-01";
	private int status = 0;
	private TextView tv_total;
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private boolean isgethttp = true;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_exchangerecord);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		exchangeprogressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.exchangeprogressBar);
		already_btn = (RadioButton) findViewById(R.id.already_btn);
		unalready_btn = (RadioButton) findViewById(R.id.unalready_btn);
		tv_start = (TextView) findViewById(R.id.tv_start);
		tv_end = (TextView) findViewById(R.id.tv_end);
		tv_total = (TextView) findViewById(R.id.tv_total);
		already_btn.setOnClickListener(this);
		unalready_btn.setOnClickListener(this);
		tv_start.setOnClickListener(this);
		tv_end.setOnClickListener(this);
		mListView = (ListView) findViewById(R.id.professor_listview);
		if (listAdapter == null) {
			listAdapter = new DZHDQListAdapter(context, DZHDQdata);
			listAdapter.setOnBottomClickListener(new OnBottomClickListener() {
				@Override
				public void onBottomClick() {
					if (hasMore) {
						curPage++;
						httpData();
					}
				}
			});
			mListView.setAdapter(listAdapter);
		}
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				if (isgethttp == true) {
					isgethttp = false;
					already_btn.setEnabled(false);
					unalready_btn.setEnabled(false);
					curPage = 1;
					DZHDQdata.clear();
					httpData();
				}

			}
		});
		status = 1;
		curPage = 1;
		DZHDQdata.clear();
		httpData();
	}

	public ExchangeRecordActivity() {}

	@Override
	public void onResume() {
		super.onResume();
		if (App.isExchangeRecordFragment) {
			status = 1;
			curPage = 1;
			DZHDQdata.clear();
			httpData();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		App.isExchangeRecordFragment = false;
	}

	@Override
	protected void setListener() {

	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.already_btn:
			if (isgethttp == true) {
				unalready_btn.setEnabled(false);
				isgethttp = false;
				already_btn.setTextColor(getResources().getColor(R.color.green));
				already_btn.setBackgroundResource(R.drawable.exchange_record_checked_bg);
				unalready_btn.setTextColor(getResources().getColor(R.color.summary_text));
				unalready_btn.setBackgroundResource(R.drawable.exchange_record_nochecked_bg);
				status = 1;
				curPage = 1;
				DZHDQdata.clear();
				tv_total.setVisibility(View.GONE);
				listAdapter.notifyDataSetChanged();
				startTime = "2000-01-01";
				endTime = "2100-01-01";
				tv_start.setText("");
				tv_end.setText("");
				httpData();
			}
			break;
		case R.id.unalready_btn:
			if (isgethttp == true) {
				isgethttp = false;
				already_btn.setEnabled(false);
				already_btn.setTextColor(getResources().getColor(R.color.summary_text));
				already_btn.setBackgroundResource(R.drawable.exchange_record_nochecked_bg);
				unalready_btn.setTextColor(getResources().getColor(R.color.green));
				unalready_btn.setBackgroundResource(R.drawable.exchange_record_checked_bg);
				status = 2;
				curPage = 1;
				DZHDQdata.clear();
				tv_total.setVisibility(View.GONE);
				listAdapter.notifyDataSetChanged();
				startTime = "2000-01-01";
				endTime = "2100-01-01";
				tv_start.setText("");
				tv_end.setText("");
				httpData();
			}
			break;
		case R.id.tv_start:
			DialogUtil.showDatePickerDialog(context, tv_start, getOldDate(tv_start),
					new OnDatePickedListener() {

						@Override
						public void pick(String pickedDate) {
							tv_start.setText(pickedDate);
							startTime = tv_start.getText().toString();
							endTime = tv_end.getText().toString();
							if (TextUtils.isEmpty(endTime)) {
								ToastUtil.showLong("请选择截止时间");
								return;
							}
							if (TimeUtil.isDayAfter(endTime, startTime, false)) {
								DZHDQdata.clear();
								listAdapter.notifyDataSetChanged();
								curPage = 1;
								httpData();
							} else {
								ToastUtil.showLong("截止时间不能早于开始时间");
							}
						}
					});
			break;
		case R.id.tv_end:
			DialogUtil.showDatePickerDialog(context, tv_end, getOldDate(tv_end), new OnDatePickedListener() {

				@Override
				public void pick(String pickedDate) {
					tv_end.setText(pickedDate);
					startTime = tv_start.getText().toString();
					endTime = tv_end.getText().toString();
					if (TextUtils.isEmpty(startTime)) {
						ToastUtil.showLong("请选择开始时间");
						return;
					}
					if (TimeUtil.isDayAfter(endTime, startTime, false)) {
						DZHDQdata.clear();
						listAdapter.notifyDataSetChanged();
						curPage = 1;
						httpData();
					} else {
						ToastUtil.showLong("截止时间不能早于开始时间");
					}
				}
			});
			break;
		default:
			break;
		}
	}

	@Override
	protected void initData() {

	}

	private static Calendar getOldDate(TextView dateTextView) {
		String oldDate = dateTextView.getText().toString().trim();
		Calendar c = null;
		if (!TextUtils.isEmpty(oldDate)) {
			String[] shengri = oldDate.split("-");
			int year = Integer.parseInt(shengri[0]);
			int month = Integer.parseInt(shengri[1]);
			if (month > 0) {
				month--;
			}
			int day = Integer.parseInt(shengri[2]);
			c = Calendar.getInstance();
			c.set(year, month, day);
		}
		return c;
	}

	private void httpData() {

		mSwipeRefreshLayout.setRefreshing(true);
		String userId = SharePreferUtil.getString(Constant.USER_ID, "");
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("status", status);
		jsonObject.addProperty("curPage", curPage);
		jsonObject.addProperty("pageSize", pageSize);
		jsonObject.addProperty("userId", userId);
		jsonObject.addProperty("startTime", startTime);
		jsonObject.addProperty("endTime", endTime);
		ZbHttpClient.postNewApi(ServerConfig.API_DHJL, jsonObject,
				new TypeGsonHttpHandler<SuperResult<DianZhuHuoDongQuanData>>() {

					@Override
					public void onSuccess(SuperResult<DianZhuHuoDongQuanData> data) {
						if (data != null) {
							if (status == 1) {// 未结算
								int alreadyPrice = data.getTotalPrice();
								if (alreadyPrice > 100000) {
									already_btn.setText("未结算" + "(" + "大于10" + "万" + ")");
								} else {
									already_btn.setText("未结算" + "(" + alreadyPrice + "元" + ")");
								}
							} else if (status == 2) {// 已结算
								int unalreadyPrice = data.getTotalPrice();
								if (unalreadyPrice > 100000) {
									unalready_btn.setText("已结算" + "(" + "大于10" + "万" + ")");
								} else {
									unalready_btn.setText("已结算" + "(" + unalreadyPrice + "元" + ")");
								}
							}
							int totalNum = data.getTotalNum();
							ArrayList<DianZhuHuoDongQuanData> HDQdata = data.getData();

							DZHDQdata.addAll(HDQdata);
							if (totalNum == DZHDQdata.size()) {
								hasMore = false;
							} else {
								hasMore = true;
							}
							listAdapter.notifyDataSetChanged();
							mSwipeRefreshLayout.setRefreshing(false);
							isgethttp = true;
							already_btn.setEnabled(true);
							unalready_btn.setEnabled(true);
							exchangeprogressBar.setVisibility(View.GONE);
						}
					}

					@Override
					public void onFailure(String errorCode, String errorMsg) {
						isgethttp = true;
						already_btn.setEnabled(true);
						unalready_btn.setEnabled(true);
						mSwipeRefreshLayout.setRefreshing(false);
						exchangeprogressBar.setVisibility(View.GONE);
						if (status == 1) {// 未结算
							already_btn.setText("未结算" + "(" + "0元" + ")");
						} else if (status == 2) {// 已结算
							unalready_btn.setText("已结算" + "(" + "0元" + ")");
						}
						ToastUtil.showShort(errorMsg);
					}
				});
	}

}
